package com.aegean.MonitorExamSYS.center.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller("ArrangeForPosition")
@Scope("prototype")
public class ArrangeForPosition extends ActionSupport implements
		ServletRequestAware {
	private Map<Integer, List<Integer>> con_PositionsMap;
	private Map<Integer, Integer> con_PositionCountMap;
	private HttpServletRequest request;
	private HttpSession session;
	private int conduction_Id;
	private int[] positionIds;
	private boolean success;
	private String msg;
	private int personCount;

	public int getPersonCount() {
		return personCount;
	}

	public void setPersonCount(int personCount) {
		this.personCount = personCount;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int[] getPositionIds() {
		return positionIds;
	}

	public void setPositionIds(int[] positionIds) {
		this.positionIds = positionIds;
	}

	public int getConduction_Id() {
		return conduction_Id;
	}

	public void setConduction_Id(int conduction_Id) {
		this.conduction_Id = conduction_Id;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		try {
			con_PositionsMap = (Map<Integer, List<Integer>>) session
					.getAttribute("confirmConductionsMap");
			con_PositionCountMap = (Map<Integer, Integer>) session
					.getAttribute("confirmPositionPersonCount");// 确定考点人数的确定
			List<Integer> positions = con_PositionsMap.get(conduction_Id);
			if (positions == null || positions.size() == 0) {
				positions = new ArrayList<Integer>();
			} else {
				positions.clear();
			}
			if(con_PositionCountMap==null){
				con_PositionCountMap=new HashMap<Integer, Integer>();
			}
			for (int i = 0; i < positionIds.length; i++) {
				positions.add(positionIds[i]);
			}
			con_PositionsMap.put(conduction_Id, positions);
			con_PositionCountMap.put(conduction_Id, personCount);
			session.setAttribute("confirmConductionsMap", con_PositionsMap);//存储考点考场的分配
            session.setAttribute("confirmPositionPersonCount", con_PositionCountMap);//存储考点考生数量的分配
			msg = "恭喜您，该考场分配成功！";
			success = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			msg = "对不起，出现系统出错，请联系管理员！";
			success = false;
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
		session = request.getSession();
	}

}
